package com.voicechat.firstmet.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.activity.PublishShareGirlsActivity;
import com.voicechat.firstmet.activity.ShareGirlsDetailsActivity;
import com.voicechat.firstmet.adapter.ShareGirlsAdapter;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.PersonDataBean;
import com.voicechat.firstmet.bean.ShareGirlsBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.FaintService;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.fragment_shar_girls
 * @ClassName: ShareGirlsFragment
 * @Description: 隐约    ------共享闺蜜
 * @Author: ljp
 * @CreateDate: 2019/1/4 19:44
 * @Version: 1.0
 */
public class ShareGirlsFragment extends BaseFragment {
    @BindView(R.id.recycler_list)
    RecyclerView recyclerList;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.floatin_button)
    ImageView floatinButton;

    private int page = 1;
    ShareGirlsAdapter shareGirlsAdapter;
    private List<ShareGirlsBean.PageBean.ListBean> tlist;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String deviceInfo;
    private String TAG = "ShareGirlsFragment";

    @Override
    protected int loadLayout() {
        return R.layout.fragment_shar_girls;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this, view);
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        initView();
    }

    /**
     * @return
     * @method
     * @description 数据控件实例化
     * @date: 2019/1/4 20:00
     * @author: ljp
     */
    private void initView() {
        tlist = new ArrayList<>();
        smartRefresh.autoRefresh();
        //下拉刷新
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initHttp();
                smartRefresh.finishRefresh(10000);
            }
        });
        //上拉加载更多
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initHttp();
                smartRefresh.finishLoadMore(10000);
            }
        });
        //设置 Header 为 贝塞尔雷达 样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        shareGirlsAdapter = new ShareGirlsAdapter(context, tlist, R.layout.item_share_girls,"");
        recyclerList.setLayoutManager(new GridLayoutManager(activity, 2));
        recyclerList.setAdapter(shareGirlsAdapter);
        shareGirlsAdapter.setItemClick(new ShareGirlsAdapter.ItemClick() {
            @Override
            public void setItemClick(int position,ShareGirlsBean.PageBean.ListBean listBean) {
                Intent intent = new Intent(activity,ShareGirlsDetailsActivity.class);
                intent.putExtra("id",listBean.getId());
                intent.putExtra("Price",listBean.getPrice());
                context.startActivity(intent);
            }
        });
        initHttp();
    }

    /**
     * @return
     * @method
     * @description 请求网络信息
     * @date: 2019/1/17 19:08
     * @author: ljp
     */
    private void initHttp() {
        FaintService.shareGirlsSelect(token, deviceInfo, mySeleInfo.getAscription(),page,mySeleInfo.getProvince()).subscribe(shareGirlsBean -> {
            Log.i(TAG, "initHttp: ----------"+shareGirlsBean.toString());
            if (shareGirlsBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = shareGirlsBean.getPage().getCount() / shareGirlsBean.getPage().getPageSize() +
                        (shareGirlsBean.getPage().getCount() % shareGirlsBean.getPage().getPageSize() == 0 ? 0 : 1);
                if (i != 0 && page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                } else {
                    if (page == 1) {
                        tlist.clear();
                        smartRefresh.finishRefresh();
                    }
                    if (shareGirlsBean.getPage().getList() != null) {
                        tlist.addAll(shareGirlsBean.getPage().getList());
                        shareGirlsAdapter.notifyDataSetChanged();
                    } else {
                    }
                    smartRefresh.finishLoadMore();
                }
            } else if (shareGirlsBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(shareGirlsBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "initHttp: ----------"+throwable.toString());
            toast_MakeText(getResources().getString(R.string.net_error));
        });
    }
    public static ShareGirlsFragment newInstance() {
        ShareGirlsFragment shareGirlsFragment = new ShareGirlsFragment();
        return shareGirlsFragment;
    }
    /**
     * @return
     * @method
     * @description 发布共享闺蜜
     * @date: 2019/1/4 19:50
     * @author: ljp
     */
    @OnClick(R.id.floatin_button)
    public void onClick() {
        //是会员用户并且已经实名认证，才可以发布
        // 会员类型  0非会员 1月会员 2年会员
        if(mySeleInfo.getvType().equals("1") || mySeleInfo.getvType().equals("2")){
            //判断是否实名认证
            if (mySeleInfo.getIsAttestation() == 0) {// 实名认证任务 0未认证 1 已审核 2 审核中 3驳回
                toast_MakeText(getResources().getString(R.string.realName_hint));
            }else {
                judeRealName();
            }
        }else {
            toast_MakeText(getResources().getString(R.string.vip_text));
        }
    }
    /**
    * @method
    * @description 判断是否实名认证
    * @date: 2019/1/28 13:45
    * @author: ljp
    * @return
    */
    private void judeRealName(){
        MeInfoService.certificationSelect(mySeleInfo.getId(), token, deviceInfo).subscribe(personDataBean -> {
            if (personDataBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                //判断state状态  认证状态 0审核中 1审核通过 2驳回
                PersonDataBean.ObjectBean object = personDataBean.getObject();
                if (object.getStatus() == 0) {
                    toast_MakeText(getResources().getString(R.string.real_name));
                } else if (object.getStatus() == 1) {
                    context.startActivity(new Intent(activity, PublishShareGirlsActivity.class));
                } else {
                    toast_MakeText(getResources().getString(R.string.realName_hint));
                }
            } else if (personDataBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(personDataBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "judeRealName: ---------" + throwable.toString());
        });
    }
}
